package com.soft.javase.day06.objectdemo;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/4/24
 */
public class StudentSystem {
    private static final int INITIAL_CAPACITY = 10;
    private Student[] students;
    private int size;

    public StudentSystem() {
        students = new Student[INITIAL_CAPACITY];
        size = 0;
    }

    // 添加学生方法
    public boolean addStudent(Student newStudent) {
        // 检查是否已存在相同学生
        for (int i = 0; i < size; i++) {
            if (students[i].equals(newStudent)) {
                return false;
            }
        }

        // 检查数组容量，必要时扩容
        if (size == students.length) {
            expandCapacity();
        }

        students[size] = newStudent;
        size++;
        return true;
    }

    // 扩容方法
    private void expandCapacity() {
        int newCapacity = students.length * 3 / 2 + 1;
        Student[] newArray = new Student[newCapacity];
        System.arraycopy(students, 0, newArray, 0, students.length);
        students = newArray;
    }

}
